<?php
class Post extends AppModel {

	var $name = 'Post';

	//The Associations below have been created with all possible keys, those that are not needed can be removed
	var $belongsTo = array(
			'Category' =>
				array('className' => 'Category',
						'foreignKey' => 'category_id',
						'conditions' => '',
						'fields' => '',
						'order' => '',
						'counterCache' => ''
				),

            'User' =>
				array('className' => 'User',
						'foreignKey' => 'user_id',
						'conditions' => '',
						'fields' => '',
						'order' => '',
						'counterCache' => ''
				),

	);

	var $hasMany = array(
			'Comment' =>
				array('className' => 'Comment',
						'foreignKey' => 'post_id',
						'conditions' => '',
						'fields' => '',
						'order' => '',
						'limit' => '',
						'offset' => '',
						'dependent' => true,
						'exclusive' => '',
						'finderQuery' => '',
						'counterQuery' => ''
				),

			'Vote' =>
				array('className' => 'Vote',
						'foreignKey' => 'post_id',
						'conditions' => '',
						'fields' => '',
						'order' => '',
						'limit' => '',
						'offset' => '',
						'dependent' => true,
						'exclusive' => '',
						'finderQuery' => '',
						'counterQuery' => ''
				),

	);

	var $hasAndBelongsToMany = array(
			'Tag' =>
				array('className' => 'Tag',
						'joinTable' => 'posts_tags',
						'foreignKey' => 'post_id',
						'associationForeignKey' => 'tag_id',
						'conditions' => '',
						'fields' => '',
						'order' => '',
						'limit' => '',
						'offset' => '',
						'uniq' => true,
						'finderQuery' => '',
						'deleteQuery' => '',
						'insertQuery' => ''
				),

	);

    var $validate = array(
      'title' => VALID_NOT_EMPTY,
	  'content' => VALID_NOT_EMPTY,
    );

    function beforeSave()
	{
		vendor("filter");
        if(isset($this->data['Post']['content'])) // filter post text
			$this->data['Post']['content'] = filterpost($this->data['Post']['content']);
		if(isset($this->data['Post']['title'])) // filter post title
			$this->data['Post']['title'] = strip_tags($this->data['Post']['title']);
	    return true;
	}

    function beforeValidate() {
        if(isset($this->data['Post']['title'])) $this->data['Post']['title'] = trim($this->data['Post']['title']);
		if(isset($this->data['Post']['content'])) $this->data['Post']['content'] = trim($this->data['Post']['content']);
        return true;
	}

  function commentcount($pid,$dir) {
	$this->id = $pid;
   if ($dir)
     	$this->saveField('replies', ($this->field('replies')+1));
	else
			$this->saveField('replies', ($this->field('replies')-1));
	}

	function getauthor($pid) {
		$this->id=$pid;
		$uid=$this->field('user_id');
		$this->User->recursive=-1;
		return $this->User->read(array("id","name","email","notify"),$uid);
	}

	function rate($pid,$v,$curvote) {
		//$this->data=array();
		//$this->id=$pid;
		//$post=$this->find('Post.id='.$pid,array('Post.points','Post.rated','Post.modified'),null,-1);
		$sql="UPDATE posts SET ";
		if($curvote>0) {
			//$this->data['Post']['points']=$post['Post']['points']-$curvote+$v;
			$sql.="points=points-$curvote+$v ";
		} else {
			//$this->data['Post']['points']=$post['Post']['points']+$v;
			$sql.="points=points+$v ";
			//$this->data['Post']['rated']=$post['Post']['rated']+1;
			$sql.=", rated=rated+1 ";
		}
		$sql.=" WHERE id=$pid";
		//$this->data['Post']['modified']=$post['Post']['modified'];		
		$this->execute($sql);
	}

}
?>